package calao.storage;

import java.util.HashMap;

public class Sequence
extends SchemaElement
{

	static HashMap<String,Sequence> MAP = new HashMap<String,Sequence>();

	boolean registered = false;
	int min = 1;
	int max = Integer.MAX_VALUE;

	/** Creates a new sequence.
	 */
	public Sequence(String name)
	{
		super(name);
	}

	@Override
	protected void register()
	throws InvalidSchemaException
	{
		Sequence current = MAP.get(name);
		if (current!=this) {
			if (current!=null) {
				throw new InvalidSchemaException(name+": sequence name already used");
			}
			MAP.put(name, this);
			registered = true;
		}
	}
	
	/** Lists bound sequences.
	 */
	public static Iterable<Sequence> getBoundSequences()
	{
		return MAP.values();
	}

	/** Gets a registered sequence.
	 * 
	 * @param name the requested sequence name.
	 * @return The sequence registered with the specified name, or null if there is
	 * no such sequence. 
	 */
	public static Sequence getSequence(String name)
	{
		return MAP.get(name);
	}
	
	public int getMaxValue() 
	{
		return max;
	}
	
	public int getMinValue() 
	{
		return min;
	}
	
	public void setMinValue(int min)
	{
		ensureNotRegistered();
		this.min = min;
	}
	
	public void setMaxValue(int max)
	{
		ensureNotRegistered();
		this.max = max;
	}
	
}
